<template>
  <div class="students_box">
    <div class="member_title">队员信息
    </div>
    <el-table
        :data="student"
        border
        style="margin: 30px 0">
      <el-table-column
          label="姓名"
          align="center">
        <template slot-scope="scope">
          <el-input
              v-if="scope.row.edit"
              v-model="scope.row.rname"
          ></el-input>
          <el-tag v-if="!scope.row.edit" type="success">{{ scope.row.rname }}</el-tag>
<!--          <span v-if="!scope.row.edit">{{ scope.row.rname }}</span>-->
        </template>
      </el-table-column>
      <el-table-column
          label="用户名"
          align="center">
        <template slot-scope="scope">
          <el-input
              v-if="scope.row.edit"
              v-model="scope.row.username"
          ></el-input>
          <span v-if="!scope.row.edit">{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="学校"
          align="center">
        <template slot-scope="scope">
          <el-input
              v-if="scope.row.edit"
              v-model="scope.row.school"
          ></el-input>
          <span v-if="!scope.row.edit">{{ scope.row.school }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="专业"
          align="center">
        <template slot-scope="scope">
          <el-input
              v-if="scope.row.edit"
              v-model="scope.row.sex"
          ></el-input>
          <el-tag v-if="!scope.row.edit">{{ scope.row.sex }}</el-tag>
<!--          <span v-if="!scope.row.edit">{{ scope.row.sex }}</span>-->
        </template>
      </el-table-column>
      <el-table-column
          label="邮箱"
          align="center">
        <template slot-scope="scope">
          <el-input
              v-if="scope.row.edit"
              v-model="scope.row.email"
          ></el-input>
          <span v-if="!scope.row.edit">{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="联系方式"
          align="center">
        <template slot-scope="scope">
          <el-input
              v-if="scope.row.edit"
              v-model="scope.row.phone"
          ></el-input>
          <span v-if="!scope.row.edit">{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
              v-show="!scope.row.edit"
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"><i class="el-icon-edit"></i></el-button>
          <el-button
              v-show="!scope.row.edit"
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index,scope.row)"><i class="el-icon-delete"></i></el-button>
          <el-button
              v-show="scope.row.edit"
              size="mini"
              @click="handleCancel(scope.$index, scope.row)"
              style="margin-left: 0"><i class="el-icon-close"></i></el-button>
          <el-button
              v-show="scope.row.edit"
              size="mini"
              type="success"
              @click="handleSave(scope.$index, scope.row)"><i class="el-icon-check"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    <div class="add_button">-->
      <el-button class="add el-icon-plus" type="success" size="max" @click="handleAddStudent"> 添加</el-button>
<!--    </div>-->
  </div>
</template>

<script>

export default {
  name: "students",
  props: ['students'],
  data() {
    return{
      nuofp: '1',  //队伍人数限制
      student: [],
      newStudent: {
        rname: '',
        username: '',
        school: '',
        sex: '', // 专业，后端字段懒得改
        email: '',
        phone: '',
        edit: false,
        Tnew: true
      }
    }
  },
  methods: {

    // 编辑
    handleEdit(index, row) {
      row.edit = true;
    },

    // 删除
    handleDelete(index,row) {
      let data = row;
      data.groupID = window.sessionStorage.getItem('groupID');
      if(this.student.length !== 1) {
        this.$confirm('确定要移除这个队员吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.deleteGroupPartner(data)
              .then(res => {
                if(res.success){
                  this.student.splice(index,1)
                  return this.$message.success('移除成功');
                } else {
                  return this.$message.warning('移除失败');
                }
              })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消移除'
          });
        });


      } else {
        this.$alert('至少需要有一名队员', '无法删除', {
          confirmButtonText: '确定',
          callback: () => {
            this.$message({
              type: 'warning',
              message: "队员不能为空"
            });
          }
        });
      }
    },

    //取消
    handleCancel(index, row) {
      if(row.Tnew){
        this.student.splice(index,1);
      }
      row.edit = false;
    },

    // 保存
    handleSave(index,row) {
      row.edit = false;
      this.student[index] = row;
      let data = row;
      data.groupID = window.sessionStorage.getItem('groupID');
      delete data.edit;
      if(row.Tnew){
        this.$api.addGroupPartner(row)
          .then(res => {
            if(res.success){
              row.Tnew = false;
              return this.$message.success('添加成功');
            } else {
              this.student.splice(index,1);
              return this.$message.warning('添加失败');
            }
          })
      } else {
        this.$api.updateGroupPartner(row) //修改信息
            .then(res =>{
              if(res.success){
                return this.$message.success('修改成功该成功');
              } else {
                return this.$message.warning('修改失败，请重试');
              }
            });
      }
    },

    // 添加
    handleAddStudent(){
      let newStudent = JSON.stringify(this.newStudent);
      const index = this.student.length
      if(this.nuofp <= index){
        console.log('人数限制：',this.nuofp);
        return this.$message.warning("超出队伍人数限制，不能在添加");
      }
      for(let i in this.student){
        if(i === index) break;
        if(this.student[i].edit) {
          this.$message({
            type: 'warning',
            message: "请先保存"
          });
          return;
        }
      }
      this.student.push(JSON.parse(newStudent))
      this.handleEdit(index,this.student[index]);
    },

  //  获取父组件传过来的值

  },
  watch: {
    students(value){
      // if(value){
        value.forEach(temp => {
          this.nuofp = window.sessionStorage.getItem('nuofp');//获取人数限制
          temp.edit = false;
          temp.Tnew = false;
          let a = JSON.stringify(temp);
          this.student.push(JSON.parse(a));
        });
      // }
    }
  }
}
</script>

<style lang="less" scoped>
.member_title{
  border-left: 15px solid darkgreen;
  padding-left: 10px;
  margin-top: 80px;
  font-size: 25px;
  font-weight: bolder;
}
</style>
